package pl.register.android.activities.gallery.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import pl.register.android.R;
import pl.register.android.gallery.LoadedImage;
import pl.register.android.widget.listview.RefreshableAdapter;

import java.util.ArrayList;

/**
 * @author Mateusz Strzelecki
 * @since 2013-05-04
 */
public class GalleryAdapter extends RefreshableAdapter<LoadedImage> {

    public GalleryAdapter(Context context) {
        super(context, new ArrayList<LoadedImage>());
        this.context = context;
    }

    public void addImage(LoadedImage image) {
        items.add(image);
        refresh();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.student_gallery_item, null);
            holder.ivImage = (ImageView) convertView.findViewById(R.id.gallery_image_view);
            holder.tvFileName = (TextView) convertView.findViewById(R.id.gallery_file_name_text_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        LoadedImage image = getItem(position);
        holder.ivImage.setImageURI(image.getUri());
        holder.tvFileName.setText(image.getFileName());
        return convertView;
    }

    public static class ViewHolder {
        public TextView tvFileName;
        public ImageView ivImage;
    }

}